package com.bigstar.lolkor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Masteries_Offense extends Activity implements OnClickListener{
	public static final String[] title = {
		"Summoner's Wrath(소환사의 진노)", "Brute Force(짐승같은 힘)", "Mental Force(정신력)", "Butcher(도살자)",
		"Alacity(민첩성)", "Sorcery(마술)","Demolitionist(철거 전문가)", 
		"Deadliness(치명타)", "Weapon Expertise(무기 전문가)", "Arcane Knowledge(태고의 지식)","Havoc(대파괴)",
		"Lethality(치명상)", "Vampirism(흡혈귀)","Blast(폭파)",
		"Sunder(방어구 가르기)","Archmage(대마법사)",
		"Executioner(처형인)"
	};
	public static final String[] text = {
		"다음 소환사 주문의 성능이 향상 됩니다:\r\n탈진 : 대상의 방어력과 마법 저항력을 10감소시킵니다.\r\n점화 : 사용할 수 없을 때 공격력과 주문력이 5증가합니다.\r\n" +
		"고양 : 공격 속도 상승량이 40%으로 증가하며 주문 사용시 얻는 주문력이 10% 추가로 증가합니다.\r\n유체화 : 이동 속도 상승량이 35% 증가합니다.",
		"공격력 +num",
		"주문력 +num",
		"기본 공격시 미니언에게 num의 추가 피해를 줍니다.",
		"공격 속도 +num%",
		"재사용 대기시간 감소 +num%",
		"기본 공격시 포탑에 num의 추가 피해를 줍니다.",
		"레벨당 공격력 +num\r\n(챔피언 18 레벨 달성시 공격력 +2.25/4.5/6.75/9",
		"방어구 관통력 +num%",
		"마법 관통력 +num%",
		"적에게 주는 피해량이 num% 증가합니다.",
		"치명타 피해 +num%",
		"생명력 흡수 +num%",
		"레벨당 주문력 +num\r\n(챔피언 18 레벨 달성시 주문력 +4.5/9/13.5/18)",
		"방어구 관통력 +num",
		"주문력이 num% 증가합니다.",
		"대상의 체력이 40% 이하일 때 주는 피해가 num% 증가합니다."
	};
	
	public static final int max_point[] = { 1,3,4,2,4,4,1,4,1,1,3,1,3,4,3,4,1 };
	public static final String[][] level = {
		{"0"},
		{"1","2","3"},
		{"1","2","3","4"},
		{"2","4"},
		{"1","2","3","4"},
		{"1","2","3","4"},
		{"10"},
		{"0.125","0.250","0.375","0.5"},
		{"10"},
		{"10"},
		{"0.5","1","1.5"},
		{"10"},
		{"1","2","3"},
		{"0.25","0.5","0.75","1"},
		{"2","4","6"},
		{"1.25","2.5","3.75","5"},
		{"6"},
	};
	public int spellid = 0;
	public static Activity context;
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
		setContentView(R.layout.masteries_offense); 
		context = this;
 		if(getIntent().getExtras() != null){
 			ImageView iv = null;
 			for(int i = 0; i < 17; i++){
 				iv = (ImageView)findViewById(R.id.o_00+i);
 				iv.setOnClickListener(this);
 			}
 			TextView tv = (TextView)findViewById(R.id.b_offense);
 			tv.setText("공격(OFFENSE):  "+Builder.getMasteriesPoint(0));
			imageChange();
 		}else{
 			ImageView iv = null;
 			for(int i = 0; i < 17; i++){
 				iv = (ImageView)findViewById(R.id.o_00+i);
 				iv.setOnClickListener(this);
 			}
 		}
	}
	
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
 		if(getIntent().getExtras() != null){
 			ImageView iv = null;
 			for(int i = 0; i < 17; i++){
 				iv = (ImageView)findViewById(R.id.o_00+i);
 				iv.setOnClickListener(this);
 			}
 			TextView tv = (TextView)findViewById(R.id.b_offense);
 			tv.setText("공격(OFFENSE):  "+Builder.getMasteriesPoint(0));
			imageChange();
 		}
    }
	
	
	public static void imageChange(){
		ImageView iv = null;
		Bitmap bitmap = null;
		TextView tv = null;
		tv = (TextView)context.findViewById(R.id.b_offense);
		tv.setText("공격(OFFENSE):  "+Builder.getMasteriesPoint(0));
		for(int i = 0; i < 17; i++){
			tv = (TextView)context.findViewById(R.id.o_text_00+i);
			tv.setText(" "+Builder.masteries[0][i]+"/"+max_point[i]);
			switch(i){
			case 4: 
			case 5:
			case 6:
				if(Builder.getMasteriesPoint(0) < 4){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 7:
			case 10:
				if(Builder.getMasteriesPoint(0) < 8){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 8: 
				if(Builder.masteries[0][4] < 4){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_00);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(0) < 8){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				iv = (ImageView)context.findViewById(R.id.m_p_00);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
				iv.setImageBitmap(bitmap);
				break;
			case 9:
				if(Builder.masteries[0][5] < 4){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_01);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(0) < 8){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				iv = (ImageView)context.findViewById(R.id.m_p_01);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
				iv.setImageBitmap(bitmap);
				break;
			case 11:
				if(Builder.masteries[0][7] < 4){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					
					iv = (ImageView)context.findViewById(R.id.m_p_02);
					iv.clearColorFilter();
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
					iv.setImageBitmap(bitmap);
					continue;
				}
				if(Builder.getMasteriesPoint(0) < 12){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				iv = (ImageView)context.findViewById(R.id.m_p_02);
				iv.setColorFilter(Color.GREEN, PorterDuff.Mode.DARKEN);
				bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mas_pro_0);
				iv.setImageBitmap(bitmap);
				break;
			case 12:
			case 13:
				if(Builder.getMasteriesPoint(0) < 12){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 14:
			case 15:
				if(Builder.getMasteriesPoint(0) < 16){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			case 16:
				if(Builder.getMasteriesPoint(0) < 20){
					iv = (ImageView)context.findViewById(R.id.o_00+i);
					iv.setColorFilter(Color.DKGRAY, PorterDuff.Mode.DARKEN);
					bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
					iv.setImageBitmap(bitmap);
					continue;
				}
				break;
			}
			iv = (ImageView)context.findViewById(R.id.o_00+i);
			iv.clearColorFilter();
			bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.o_00+i);
			iv.setImageBitmap(bitmap);
		}
	}
	

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.b_champ_m_minus:
			if(Builder.masteries[0][spellid] > 0){
				Builder.masteries[0][spellid]--;
				set();
			}
			break;
		case R.id.b_champ_m_plus:
			if(Builder.getMasteriesCount() >= 30){
				Toast.makeText(this, "포인트 30 다씀", Toast.LENGTH_LONG).show();
				return;
			}
			switch(spellid){
			case 4: 
			case 5:
			case 6:
				if(Builder.getMasteriesPoint(0) < 4){
					return;
				}
				break;
			case 7:
			case 10:
				if(Builder.getMasteriesPoint(0) < 8){
					return;
				}
				break;
			case 8: 
				if(Builder.masteries[0][4] < 4){
					return;
				}
				if(Builder.getMasteriesPoint(0) < 8){
					return;
				}
				break;
			case 9:
				if(Builder.masteries[0][5] < 4){
					return;
				}
				if(Builder.getMasteriesPoint(0) < 8){
					return;
				}
				break;
			case 11:
				if(Builder.masteries[0][7] < 4){
					return;
				}
				if(Builder.getMasteriesPoint(0) < 12){
					return;
				}
				break;
			case 12:
			case 13:
				if(Builder.getMasteriesPoint(0) < 12){
					return;
				}
				break;
			case 14:
			case 15:
				if(Builder.getMasteriesPoint(0) < 16){
					return;
				}
				break;
			case 16:
				if(Builder.getMasteriesPoint(0) < 20){
					return;
				}
				break;
			}
			if(Builder.masteries[0][spellid] < max_point[spellid]){
				Builder.masteries[0][spellid]++;
				set();
			}
			break;
		default:
			dialog(v.getId() - R.id.o_00);
			break;
		}
	}
	
	public void set(){
		TextView tv = (TextView)findViewById(R.id.b_offense);
		tv.setText("공격(OFFENSE):  "+Builder.getMasteriesPoint(0));
		tv = (TextView)findViewById(R.id.o_text_00+spellid);
		tv.setText(" "+Builder.masteries[0][spellid]+"/"+max_point[spellid]);
		tv = (TextView) layout.findViewById(R.id.b_champ_m_text);
		tv.setText("단계: "+Builder.masteries[0][spellid]+"/"+max_point[spellid]+"\r\n\r\n"+text[spellid]);
		String limit = "";
		switch(spellid){
		case 4:
		case 5:
		case 6: limit = "\r\nOffense 4포인트 필요\r\n"; break;
		case 7:
		case 10: limit = "\r\nOffense 8포인트 필요\r\n"; break;	
		case 8: limit = "\r\nOffense 8포인트 필요\r\nAlacrity 4포인트 필요\r\n"; break;
		case 9: limit = "\r\nOffense 8포인트 필요\r\nSorcery 4포인트 필요\r\n"; break;
		case 11: limit = "\r\nOffense 12포인트 필요\r\nDeadliness 4포인트 필요\r\n"; break;
		case 12:
		case 13: limit = "\r\nOffense 12포인트 필요\r\n"; break;
		case 14:
		case 15: limit = "\r\nOffense 16포인트 필요\r\n"; break;
		case 16: limit = "\r\nOffense 20포인트 필요\r\n"; break;
		}
		if(Builder.masteries[0][spellid] >= max_point[spellid]){
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[0][spellid]-1]);
			tv.setText("단계: "+Builder.masteries[0][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t);	
		}else if(Builder.masteries[0][spellid] == 0){
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[0][spellid]]);
			tv.setText("단계: "+Builder.masteries[0][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t);	
		}else{
			String t = text[spellid];
			t = t.replaceAll("num", level[spellid][Builder.masteries[0][spellid]-1]);
			String next = text[spellid];
			next = next.replaceAll("num", level[spellid][Builder.masteries[0][spellid]]);
			tv.setText("단계: "+Builder.masteries[0][spellid]+"/"+max_point[spellid]+"\r\n"+limit+"\r\n"+t
			          +"\r\n\r\n다음 단계:\r\n"+next);	
		}
	}
	
	View layout;
	
	private void dialog(int id) {
 		if(getIntent().getExtras() != null){
 			spellid = id;
 			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
 			layout = inflater.inflate(R.layout.build_masteries,(ViewGroup) findViewById(R.id.b_champ_m));
 			set();
 			Button bt = (Button)layout.findViewById(R.id.b_champ_m_minus);
 			bt.setOnClickListener(this);
 			bt = (Button)layout.findViewById(R.id.b_champ_m_plus);
 			bt.setOnClickListener(this);
 			AlertDialog.Builder alt_bld = new AlertDialog.Builder(getParent()); 
 			alt_bld
 			.setView(layout)
 			.setCancelable(false).setNegativeButton("닫기",  
 					new DialogInterface.OnClickListener() {
 				public void onClick(DialogInterface dialog, int id) {
 					imageChange();
 					dialog.cancel();
 				}});  
 			AlertDialog alert = alt_bld.create();  
 				alert.setTitle(title[id]);
 				alert.setIcon(R.drawable.o_00+id);
 				alert.show(); 
 		}else{
			String num = "";
			String t = text[id];
			for(int i = 0; i < level[id].length; i++){
				num = num+level[id][i];
				if(level[id].length == 1){
				}else if(i+1 < level[id].length){
					num = num+"/";
				}
			}
			t = t.replaceAll("num", num);
			TextView tv = new TextView(this);
			tv.setText(t);
			tv.setTextSize(14);
 			AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); 
 			alt_bld.
 			setView(tv)
 			.setNegativeButton("닫기",  
 					new DialogInterface.OnClickListener() {
 				public void onClick(DialogInterface dialog, int id) {
 					dialog.cancel();
 				}});  
 			AlertDialog alert = alt_bld.create();  
 				alert.setTitle(title[id]);
 				alert.setIcon(R.drawable.o_00+id);
 				alert.show(); 	
 		}
	}
}
